package com.android.myapplication.third.greendao;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.myapplication.App;
import com.android.myapplication.R;

import androidx.appcompat.app.AlertDialog;

public class StudentDaoActivity extends Activity {
    ListView mList;
    EditText mEditText;
    ArrayAdapter<StudentBean2> mAdapter;
    TeacherBean mTeacherBean;
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_greendao);
        ((TextView)findViewById(R.id.title)).setText("学生");
         mList  = findViewById(R.id.list);
         mEditText = findViewById(R.id.ed_name);
        Long id = getIntent().getLongExtra("id", 0);
        if (id > 0){
            mTeacherBean =  ((App)getApplication()).getDaoSession().getTeacherBeanDao().load(id);
        }
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    add();
                    return true;
                }
                return false;            }
        });
        mList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final StudentBean2 studentBean2 = mAdapter.getItem(position);
                new AlertDialog.Builder(StudentDaoActivity.this).setTitle("删除").setMessage("确认删除"+studentBean2.getName()+"吗？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((App)getApplication()).getDaoSession().getStudentBean2Dao().delete(studentBean2);

                        mTeacherBean.resetStudentList();
                        upList();
                        dialog.dismiss();
                    }
                }).show();
                return false;
            }
        });
        upList();
    }

    public void onBackPressed(){
        super.onBackPressed();
        finish();
    }

    private void add(){
        StudentBean2 studentBean2 = new StudentBean2();
        studentBean2.setName(mEditText.getText().toString());
        studentBean2.setFk_teacher_id(mTeacherBean.id);
        ((App)getApplication()).getDaoSession().getStudentBean2Dao().insert(studentBean2);
        mTeacherBean.resetStudentList();
        mEditText.setText("");
        upList();
    }
    private void upList(){
        mAdapter = new ArrayAdapter<StudentBean2>(this,android.R.layout.simple_list_item_1, mTeacherBean.getStudentList());
        mList.setAdapter(mAdapter);

    }
}
